package ui.login
{
	
	import com.ricky.controls.Box;
	import com.ricky.controls.Button;
	import com.ricky.controls.Image;
	import com.ricky.controls.Input;
	import com.ricky.controls.Label;
	import com.ricky.controls.TextArea;
	
	import fgame.GameState;
	import fgame.graphic.Graphic;
	import fgame.unit.Actor3D;
	
	import flare.core.Pivot3D;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	
	import model.ConfigUnitHelper;
	import model.config.Unit;
	
	import ricky.loader.MultipleLoader;
	import ricky.loader.Request;
	
	import ui.BaseView;

	/**
	 * <h1>创建角色UI</h1>
	 * @author:pu
	 * @data:2010.9.15
	 */
	public class LoginView extends BaseView
	{
		public function LoginView()
		{
			super();
			this.width=1000;
			this.height=700;
		}
		
		//override

		public function get unitID():String
		{
//			_unitID=_ddl.selectedValue.unitID;
			return _unitID;
		}

		public function set unitID(value:String):void
		{
			_unitID = value;
		}

		override protected function createChildren():void
		{
			_textArea=new TextArea();
			_textArea.htmlText=CLocale.getValue("createHeroInfo");
//			_textArea.width=200;	
//			_textArea.height=100;
			_textArea.x=320;
			_textArea.y=20;
			
			input=new Input();
			input.text="玩家"+new Date().minutes;
			input.width=120;
			input.x=120;
//			input.textAlign="center";
			input.y=480;
			this.addChild(input);
			
			l=new Label();
			l.name="selected";
			l.text="角色介绍";
//			l.textAlign="center";
			l.x=input.x;
			l.y=120;
			l.width=120;
			
			var allow:Array=CLocale.getValue("allowRole").toString().split(",");
			var allowData:Array=[];
			var hero:Object;
			for each(var v:String in allow)
			{
				hero=ConfigUnitHelper.getHeroConfig(v);
				if(hero)
					allowData.push(hero);
			}
			
			actor=new Actor3D();
			_rolePanel=new Box();
			_rolePanel.style={"paddingTop":8,"paddingRight":8,"paddingBottom":8,"paddingLeft":8,"layout":"tileLayout","tileWidth":140,"tileHeight":140};
			_rolePanel.background=false;
			_rolePanel.width=560;
			_rolePanel.height=400;
			_rolePanel.y=l.y;
	
			_rolePanel.x=_textArea.x-8;

			if(allowData){
				for each(var herou:Unit in allowData)
				{

						var tp:Image=new  Image();
						tp.tag=herou;
						tp.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
						tp.addEventListener(MouseEvent.MOUSE_OUT,onMoveOut);
						tp.addEventListener(MouseEvent.MOUSE_DOWN,onClick);

						var req:Request=new Request(herou.uicon+"128X128.png",onImgComp);
						req.head=tp;
						MultipleLoader.instance.addReqToQueue(req);
						_rolePanel.addChild(tp);

				}
			}

//			
			btnbegin=new Button();
			btnbegin.text="创建角色";
//			btnbegin.color=0xff0000;
//			btnbegin.fontSize=14;
			btnbegin.width=120;
//			btnbegin.textAlign="center";
			btnbegin.y=input.y+30;
			btnbegin.x=input.x;
			btnbegin.click=beginGame;
			btnbegin.enabled=false;
			btnbegin.initialize();
				
			this.addChild(_textArea);
			this.addChild(_rolePanel)
			this.addChild(btnbegin);
			this.addChild(l);
		}
		
		private function onImgComp(e:Request):void
		{
			var img:Image=e.head;
			var bit:Bitmap=new Bitmap(e.data.bitmapData);
			img.addChild(bit);
		}
		
		protected function onClick(e:MouseEvent):void
		{
			var unit:Unit=e.target.tag;
			if(_lastselected)
				_lastselected.filters=[];
			
			btnbegin.enabled=true;
			l.text="模型加载中...";
			
			unitID=unit.unitID;
				
			actor.data=unit;

			actor.graphic.wear(unit.model,null,onLoaded);

			var piv:Pivot3D=actor.graphic as Pivot3D;
			
			GameState.inst.viewer3D.addChild(piv);
			
			e.target.filters=[new GlowFilter(0xff0000,0.9)];
			_lastselected=e.target as Sprite;
			
		}
		
		protected function onLoaded(e:Graphic):void
		{
			l.text=e.actor.data.revivetip;
//			var s:Number=e.actor.data.scale;
//			e.setScale(100,100,100);
//			e.lookAt(0,0,0);
			e.rotateY(180);
		}
	

		protected function beginGame(e:Event):void
		{
			this.dispatchEvent(new Event("createSure"));
		}
		
		protected function onMouseOver(e:MouseEvent):void
		{
			if(e.target is Sprite)
			{
				e.target.filters=[new GlowFilter(0xffff00,0.9)];
//				TweenMax.to(e.target,0.2,{glowFilter:{color:0xffffff,blurX:5,blurY:5,alpha:1,strength:2,quality:1}});
			}
		}
		
		
		protected function onMoveOut(e:Event):void
		{
			if(e.target is Sprite)
			{
				if(_lastselected!=e.target)
					e.target.filters=null;
//				TweenMax.to(e.target,0.2,{glowFilter:{alpha:0,strength:0}});
			}
		}

		public function get chooseRole():String
		{
			return _chooseRole;
		}

		public function set chooseRole(value:String):void
		{
			_chooseRole = value;
		}

		public function get choose():Graphic
		{
			return _choose;
		}

		public function getHeroName():String
		{
			return input.text;
		}
		
		override public function dispose():void
		{
			super.dispose();
			if(this.parent)
				this.parent.removeChild(this);
			GameState.inst.viewer3D.removeChild(actor.graphic as Pivot3D);
			actor=null;

		}
		//private
		private var _unitID:String;//配置数据
//		private var _ddl:DropDownList;
		private var _lastselected:Sprite;
		private var _chooseRole:String;
		private var _textArea:TextArea;//游戏介绍
		private var _rolePanel:Box;
		private var _choose:Graphic;
//		private var allRoles:Dictionary;
		private var btnbegin:Button;

		private var l:Label;
		private var input:Input;
		
		private var actor:Actor3D;
	}
}